/*
 * can.c
 *
 *  Created on: 2025年4月25日
 *      Author: Administrator
 */
#include <can/can.h>
flexcan_frame_t can1_recv_data;
flexcan_frame_t can2_recv_data;
void Can1Init(uint32_t bitrate)
{
	flexcan_config_t flexcanConfig;
	flexcan_rx_mb_config_t mbConfig;
	uint8_t node_type;
	/*Clock setting for FLEXCAN*/
	clock_root_config_t rootCfg = {0};
	rootCfg.mux                 = FLEXCAN_CLOCK_SOURCE_SELECT;
	rootCfg.div                 = FLEXCAN_CLOCK_SOURCE_DIVIDER;
	CLOCK_SetRootClock(kCLOCK_Root_Can1, &rootCfg);
	FLEXCAN_GetDefaultConfig(&flexcanConfig);
	flexcanConfig.bitRate = bitrate;
	flexcanConfig.disableSelfReception=true;
	flexcanConfig.enableIndividMask=true;
	flexcan_timing_config_t timing_config;
	memset(&timing_config, 0, sizeof(flexcan_timing_config_t));
	if (FLEXCAN_CalculateImprovedTimingValues(CAN1, flexcanConfig.bitRate, CAN1_CLK_FREQ, &timing_config))
	{
		/* Update the improved timing configuration*/
		memcpy(&(flexcanConfig.timingConfig), &timing_config, sizeof(flexcan_timing_config_t));
	}
	else
	{
		printf("No found Improved Timing Configuration. Just used default configuration\r\n\r\n");
	}
	FLEXCAN_Init(CAN1, &flexcanConfig, CAN1_CLK_FREQ);
	FLEXCAN_SetTxMbConfig(CAN1, TX_MESSAGE_BUFFER_NUM, true);
	FLEXCAN_SetTxMbConfig(CAN1, TX_MESSAGE_BUFFER_NUM-1, true);
	mbConfig.format = kFLEXCAN_FrameFormatStandard;
	mbConfig.type   = kFLEXCAN_FrameTypeData;
	mbConfig.id     = FLEXCAN_ID_STD(0x1A);
	FLEXCAN_SetRxMbConfig(CAN1, RX_MESSAGE_BUFFER_NUM, &mbConfig, true);
	FLEXCAN_EnableMbInterrupts(CAN1, (uint64_t)1U << RX_MESSAGE_BUFFER_NUM);
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x2A,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x31,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x32,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x33,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x3A,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x61,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x62,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x63,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x71,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x72,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x73,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x74,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x81,0,0));
	FLEXCAN_SetRxIndividualMask(CAN1,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x82,0,0));
	(void)EnableIRQ(CAN1_IRQn);
	__NVIC_SetPriority(CAN1_IRQn, 6);
}
void Can2Init(uint32_t bitrate)
{
	flexcan_config_t flexcanConfig;
	flexcan_rx_mb_config_t mbConfig;
	uint8_t node_type;
	/*Clock setting for FLEXCAN*/
	clock_root_config_t rootCfg = {0};
	rootCfg.mux                 = FLEXCAN_CLOCK_SOURCE_SELECT;
	rootCfg.div                 = FLEXCAN_CLOCK_SOURCE_DIVIDER;
	CLOCK_SetRootClock(kCLOCK_Root_Can2, &rootCfg);
	FLEXCAN_GetDefaultConfig(&flexcanConfig);
	flexcanConfig.bitRate = bitrate;
	flexcanConfig.disableSelfReception=true;
	flexcanConfig.enableIndividMask=true;
	flexcan_timing_config_t timing_config;
	memset(&timing_config, 0, sizeof(flexcan_timing_config_t));
	if (FLEXCAN_CalculateImprovedTimingValues(CAN2, flexcanConfig.bitRate, ((CLOCK_GetRootClockFreq(kCLOCK_Root_Can2) / 100000U) * 100000U), &timing_config))
	{
		/* Update the improved timing configuration*/
		memcpy(&(flexcanConfig.timingConfig), &timing_config, sizeof(flexcan_timing_config_t));
	}
	else
	{
		printf("No found Improved Timing Configuration. Just used default configuration\r\n\r\n");
	}
	FLEXCAN_Init(CAN2, &flexcanConfig, ((CLOCK_GetRootClockFreq(kCLOCK_Root_Can2) / 100000U) * 100000U));
	FLEXCAN_SetTxMbConfig(CAN2, TX_MESSAGE_BUFFER_NUM, true);
	FLEXCAN_SetTxMbConfig(CAN2, TX_MESSAGE_BUFFER_NUM-1, true);
	mbConfig.format = kFLEXCAN_FrameFormatStandard;
	mbConfig.type   = kFLEXCAN_FrameTypeData;
	mbConfig.id     = FLEXCAN_ID_STD(0x1A);
	FLEXCAN_SetRxMbConfig(CAN2, RX_MESSAGE_BUFFER_NUM, &mbConfig, true);
	FLEXCAN_EnableMbInterrupts(CAN2, (uint64_t)1U << RX_MESSAGE_BUFFER_NUM);
	FLEXCAN_SetRxIndividualMask(CAN2,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x82,0,0));
	FLEXCAN_SetRxIndividualMask(CAN2,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x64,0,0));
	FLEXCAN_SetRxIndividualMask(CAN2,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x65,0,0));
	FLEXCAN_SetRxIndividualMask(CAN2,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x66,0,0));
	FLEXCAN_SetRxIndividualMask(CAN2,RX_MESSAGE_BUFFER_NUM,FLEXCAN_RX_MB_EXT_MASK(0x83,0,0));
	(void)EnableIRQ(CAN2_IRQn);
	__NVIC_SetPriority(CAN2_IRQn, 6);
}
void Can1Send(flexcan_frame_t can1_send_data)
{
	FLEXCAN_WriteTxMb(CAN1, TX_MESSAGE_BUFFER_NUM, &can1_send_data);
	vTaskDelay(1);
}
void Can2Send(flexcan_frame_t can2_send_data)
{
	FLEXCAN_WriteTxMb(CAN2, TX_MESSAGE_BUFFER_NUM, &can2_send_data);
	vTaskDelay(1);
}
void CAN1_IRQHandler()
{
	uint8_t can1_recv_buff[8];
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	if(0U != FLEXCAN_GetMbStatusFlags(CAN1, (uint64_t)1U << RX_MESSAGE_BUFFER_NUM))
	{
		FLEXCAN_ReadRxMb(CAN1, RX_MESSAGE_BUFFER_NUM, &can1_recv_data);
		can1_recv_buff[0] = can1_recv_data.dataByte0;
		can1_recv_buff[1] = can1_recv_data.dataByte1;
		can1_recv_buff[2] = can1_recv_data.dataByte2;
		can1_recv_buff[3] = can1_recv_data.dataByte3;
		can1_recv_buff[4] = can1_recv_data.dataByte4;
		can1_recv_buff[5] = can1_recv_data.dataByte5;
		can1_recv_buff[6] = can1_recv_data.dataByte6;
		can1_recv_buff[7] = can1_recv_data.dataByte7;
		xEventGroupSetBitsFromISR(can_recv_event_handle, 0x1, &xHigherPriorityTaskWoken);
		if(FLEXCAN_ID_STD(0x1A) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_ebv11_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x2A) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_2A, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x31) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_eiu31_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x32) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_eiu32_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x33) == can1_recv_data.id)
		{
			xQueueOverwriteFromISR(can1_recv_queue_eiu33_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		}
		else if(FLEXCAN_ID_STD(0x3A) == can1_recv_data.id)
		{
			xQueueOverwriteFromISR(can1_recv_queue_eiu3a_a, can1_recv_buff, &xHigherPriorityTaskWoken);
			if(can1_recv_buff[0]==0xA3)
				xQueueOverwriteFromISR(can1_recv_queue_eiu3_a, can1_recv_buff, &xHigherPriorityTaskWoken);
			else if(can1_recv_buff[0]==0xA4)
				xQueueOverwriteFromISR(can1_recv_queue_eiu4_a, can1_recv_buff, &xHigherPriorityTaskWoken);
			else if(can1_recv_buff[0]==0xA5)
				xQueueOverwriteFromISR(can1_recv_queue_eiu5_a, can1_recv_buff, &xHigherPriorityTaskWoken);
			else if(can1_recv_buff[0]==0xA6)
				xQueueOverwriteFromISR(can1_recv_queue_eiu6_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		}
		else if(FLEXCAN_ID_STD(0x61) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_bpcm61_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x62) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_bpcm62_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x63) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_bpcm63_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x71) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_bccm71_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x72) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_bccm72_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x73) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_bccm73_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x74) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_bccm74_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x81) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_ibcm81_a, can1_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x82) == can1_recv_data.id)
			xQueueOverwriteFromISR(can1_recv_queue_ibcm82_a, can1_recv_buff, &xHigherPriorityTaskWoken);

		portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
		FLEXCAN_ClearMbStatusFlags(CAN1, (uint64_t)1U << RX_MESSAGE_BUFFER_NUM);
    }
	SDK_ISR_EXIT_BARRIER;
}
void CAN2_IRQHandler()
{
	uint8_t can2_recv_buff[8];
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	if(0U != FLEXCAN_GetMbStatusFlags(CAN2, (uint64_t)1U << RX_MESSAGE_BUFFER_NUM))
	{
		FLEXCAN_ReadRxMb(CAN2, RX_MESSAGE_BUFFER_NUM, &can2_recv_data);
		can2_recv_buff[0] = can2_recv_data.dataByte0;
		can2_recv_buff[1] = can2_recv_data.dataByte1;
		can2_recv_buff[2] = can2_recv_data.dataByte2;
		can2_recv_buff[3] = can2_recv_data.dataByte3;
		can2_recv_buff[4] = can2_recv_data.dataByte4;
		can2_recv_buff[5] = can2_recv_data.dataByte5;
		can2_recv_buff[6] = can2_recv_data.dataByte6;
		can2_recv_buff[7] = can2_recv_data.dataByte7;
		xEventGroupSetBitsFromISR(can_recv_event_handle, 0x2, &xHigherPriorityTaskWoken);
		if(FLEXCAN_ID_STD(0x1A) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_ebv11_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x2A) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_2B, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x31) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_eiu31_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x32) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_eiu32_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x33) == can2_recv_data.id)
		{
			xQueueOverwriteFromISR(can2_recv_queue_eiu33_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		}
		else if(FLEXCAN_ID_STD(0x3A) == can2_recv_data.id)
		{
			xQueueOverwriteFromISR(can2_recv_queue_eiu3a_b, can2_recv_buff, &xHigherPriorityTaskWoken);
			if(can2_recv_buff[0]==0xA3)
				xQueueOverwriteFromISR(can2_recv_queue_eiu3_b, can2_recv_buff, &xHigherPriorityTaskWoken);
			else if(can2_recv_buff[0]==0xA4)
				xQueueOverwriteFromISR(can2_recv_queue_eiu4_b, can2_recv_buff, &xHigherPriorityTaskWoken);
			else if(can2_recv_buff[0]==0xA5)
				xQueueOverwriteFromISR(can2_recv_queue_eiu5_b, can2_recv_buff, &xHigherPriorityTaskWoken);
			else if(can2_recv_buff[0]==0xA6)
				xQueueOverwriteFromISR(can2_recv_queue_eiu6_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		}
		else if(FLEXCAN_ID_STD(0x61) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_bpcm61_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x62) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_bpcm62_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x63) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_bpcm63_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x71) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_bccm71_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x72) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_bccm72_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x73) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_bccm73_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x74) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_bccm74_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x81) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_ibcm81_b, can2_recv_buff, &xHigherPriorityTaskWoken);
		else if(FLEXCAN_ID_STD(0x82) == can2_recv_data.id)
			xQueueOverwriteFromISR(can2_recv_queue_ibcm82_b, can2_recv_buff, &xHigherPriorityTaskWoken);

		portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
		FLEXCAN_ClearMbStatusFlags(CAN2, (uint64_t)1U << RX_MESSAGE_BUFFER_NUM);
    }
	SDK_ISR_EXIT_BARRIER;
}
